//
//  XJNearByAddresNavView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/9.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  h: 50

#import "XJNearByAddresNavView.h"

@interface XJNearByAddresNavView()<UISearchBarDelegate>
/**左边城市按钮点击可以显示更多城市*/
@property (nonatomic, strong) UILabel *leftLabel;
/**中间搜索框*/
@property (nonatomic, strong) UISearchBar *centerSearchBar;
/***/
@property (nonatomic, strong) UIButton *cancelButton;


@end

@implementation XJNearByAddresNavView

#pragma mark - 一 lazy
/**高度动态计算*/
- (UILabel *)leftLabel {
    if (!_leftLabel) {
        _leftLabel = [UILabel new];
        _leftLabel.text = @"杭州市";
        
        _leftLabel.userInteractionEnabled = YES;
        //添加一个手势
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLeftCityLbAction)];
        [_leftLabel addGestureRecognizer:tapGes];
    }
    return _leftLabel;
}

//系统的search超级丑。除非自定义
/**根据上面的👆label的高度动态的变化*/
//- (UISearchBar *)centerSearchBar {
//    if (!_centerSearchBar) {
//        _centerSearchBar = [UISearchBar new];
//        _centerSearchBar.placeholder = @"小区/写字楼/学校";
//    }
//    return _centerSearchBar;
//}

- (UISearchBar *)centerSearchBar {
    if (!_centerSearchBar) {
        // 创建对象
        _centerSearchBar = [[UISearchBar alloc] init];
        
        // 设置属性
        _centerSearchBar.delegate =self;
        _centerSearchBar.placeholder = @"小区/写字楼/学校";
        _centerSearchBar.layer.cornerRadius = 15;
        _centerSearchBar.layer.masksToBounds = YES;
        
        //UISearchBar 默认有一个灰色的view  使用backgroundImage 可以去掉
        _centerSearchBar.backgroundImage = [self imageFromColor:[UIColor whiteColor]];
        //        _searchBar.backgroundColor = [UIColor whiteColor]; // 没用
        
        // 放大镜icon
        UIImage *leftImage = [UIImage imageNamed:@"nav_search"];
        [_centerSearchBar setImage:leftImage forSearchBarIcon:(UISearchBarIconSearch) state:(UIControlStateNormal)];
        
        // 清除按钮
        UIImage *clearImage = [UIImage imageNamed:@"close_black"]; // 叉叉
        [_centerSearchBar setImage:clearImage forSearchBarIcon:(UISearchBarIconClear) state:(UIControlStateNormal)];
        
        // 固定写法:修改searchBar输入框的样式
        UITextField *searchField = [_centerSearchBar valueForKey:@"_searchField"];
        searchField.enablesReturnKeyAutomatically = YES;
        [searchField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [searchField setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
        //        searchField.backgroundColor = kSearchBarTFDColor; //qi
        searchField.backgroundColor = XJCOLOR_RGB(240, 240, 240);
        searchField.textColor = [UIColor lightGrayColor];
    }
    return _centerSearchBar;
}

/**宽高固定。(30,30)*/
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_cancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_cancelButton addTarget:self action:@selector(clickCancelButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _cancelButton;
}
#pragma mark - 二 init

//h:50
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.leftLabel];
    [self addSubview:self.centerSearchBar];
    [self addSubview:self.cancelButton];
    self.centerSearchBar.backgroundColor = [UIColor whiteColor];
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat leftLbW = [self rb_widhtWithString:self.leftLabel.text];
    
    //杭州市    搜索框    取消
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(leftLbW, 40));
    }];
    
    
    //中间搜索框
    [self.centerSearchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftLabel.mas_right);
        make.right.equalTo(self.cancelButton.mas_left);
        make.centerY.equalTo(self.mas_centerY);
        make.height.equalTo(@(40));
    }];
    
    //右边取消按钮(40，40)
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.right.equalTo(self.mas_right).offset(-10);
    }];
}


#pragma mark - 四 点击事件

- (void)tapLeftCityLbAction {
    if (self.xjNearByAddressNavViewClickLeftCityBlock) {
        self.xjNearByAddressNavViewClickLeftCityBlock();
    }
}

- (void)clickCancelButtonAction {
    XJLog(@"点击了取消");
    if (self.xjNearByAddressNavViewClickCancelButtonBlock) {
        self.xjNearByAddressNavViewClickCancelButtonBlock();
    }
}

#pragma mark - 五 UISearchBarDelegate

//点击清除按钮 || 呼出键盘
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    return YES;
}

// 点击搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    [searchBar resignFirstResponder]; // 回收键盘
    
    XJLog(@"搜索的内容是:%@",searchBar.text);
    if (self.xjNearByAddressNavViewSearchBlock) {
        self.xjNearByAddressNavViewSearchBlock(searchBar.text);
    }
    
}

#pragma mark - 六 接口
- (void)reValueAndLayoutWithCityName:(NSString *)city {
    if ([NSString isEmptyString:city]) return;
    
    self.leftLabel.text = city;
    
    //重新布局
    CGFloat lb_W = [self rb_widhtWithString:city];
    
    //无效
    XJLog(@"%f",lb_W);
    [self.leftLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(lb_W, 40));
    }];
    
    //中间搜索框
    [self.centerSearchBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftLabel.mas_right);
        make.right.equalTo(self.cancelButton.mas_left);
        make.centerY.equalTo(self.mas_centerY);
        make.height.equalTo(@(40));
    }];

}

#pragma mark - 七 私有方法
/**根据文字计算label的宽度 默认字体18.0*/
- (CGFloat)rb_widhtWithString:(NSString *)str {
    CGSize  lbSize = [self.leftLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0]}]; //系统默认lb的font 应该是17.0
    
    CGFloat leftLbW = lbSize.width;
    
    return leftLbW;
}


// 将color 转为image
- (UIImage *)imageFromColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    // 获取当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 设置颜色
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    // 填充矩形区域
    CGContextFillRect(context, rect);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return img;
}


#pragma mark - 操作流程
/**
 一 UI:
 左边一个Label(Button) 点击显示城市列表(需要重新写)(又是一个单独的自定义view可以通过控制 hidden来做) 选择城市后label的宽度动态计算(根据文字动态显示宽度) 、选择城市后回调到控制器通知百度地图更新定位城市。相当于百度地图搜索附近城市。 操作较多。
 
 
 中间一个搜索框: 搜索功能
 
 右边一个取消按钮: 返回到上一个界面。
 
 */



@end
